class Solution(object):
    def makeLargestSpecial(self, s):
        def dfs(t):
            n = len(t)
            st = []
            prefix = 0
            left = 0
            for i in range(n):
                if t[i] == '1':
                    prefix += 1
                else:
                    prefix -= 1
                if prefix == 0:
                    st.append(t[left: i + 1])
                    left = i + 1
            if len(st) == 1:
                if len(st[0]) == 2:
                    return t
                else:
                    return '1' + dfs(t[1:-1]) + '0'
            else:
                next_st = [dfs(c) for c in st]
                next_st.sort(reverse=True)
                return ''.join(next_st)
        return dfs(s)


data = Solution()
s = "11011000"
print(data.makeLargestSpecial(s))
